# coding: utf-8

'''
http://wiki.it.yzs.io:8090/pages/viewpage.action?pageId=37188251

API查询数据来源:
Mongo的usk-shop库，uc-statics(热门, 猜你喜欢接口).
MySQL的open_custom和usk_shop库.

'''
import time
import json
import base64
import hashlib
import urllib2

from conf.global_config import get_env_vars

from utils.req import *
from utils.jktest import *

####################################
def hot_skill():
    url = "http://%s/%s/rest/v1/skill/hot_list?" % (Server, Project)
    params = {
        "appkey" : AppKey,
        # "count" : 5
    }
    ret = send_get(url, params)

def guess_skill():
    url = "http://%s/%s/rest/v1/skill/guess_list?" % (Server, Project)
    params = {
        "appkey" : AppKey,
        "udid" : UDID,
        # "count" : 5
    }
    ret = send_get(url, params)

def query_skill():
    url = "http://%s/%s/rest/v1/skill/query?" % (Server, Project)
    params = {
        "appkey" : AppKey,
        "udid" : UDID,
        "categoryId" : 1, # 技能类别ID
        "keyword" : "", # 搜索关键字
        "skillOrigin" : 1, # 1：官方技能，2：第三方技能
        "tags" : "", # 标签名（多个以英文逗号隔开）
        "type" : 1, # 1：自定义 2：智能问答 3：智能播报

    }
    ret = send_get(url, params)

def detail_skill():
    url = "http://%s/%s/rest/v1/skill/detail?" % (Server, Project)
    params = {
        "skillId" : skillId,
        "skillVersion" : ""
        }
    ret = send_get(url, params)


def skill_app_count():
    # 此接口根据技能id和技能版本号查询该技能关联app的数量
    # skill id 见usk-shop > skill表的skillid(内置技能都是数字)
    url = "http://%s/%s/rest/v1/skillApp/count?" % (Server, Project)
    params = {
        "skillId" : skillId,
        "skillVersion" : skillVersion
        }
    ret = send_get(url, params)

def skill_app_info():
    # 此接口根据技能id和技能版本号查询该技能的信息以及关联的app的数量
    url = "http://%s/%s/rest/v1/skillApp/skill_app_info?" % (Server, Project)
    params = {
        "skillId" : skillId,
        "skillVersion" : skillVersion
    }
    ret = send_get(url, params)

######################
def init_test_data():
    return {
    # 1. 热门技能列表
    "rest/v1/skill/hot_list" : {
        "input" : {
            "appkey" : AppKey,
        },
        "output" : "errorCode=0",
    },
    # 2. 猜你喜欢技能列表
    "rest/v1/skill/guess_list" : {
        "input" : {
            "appkey" : AppKey,
            "udid" : UDID,
        },
        "output" : "errorCode=0",
    },

    # 3. 分类查询技能列表
    "rest/v1/skill/query" : {
        "input" : {
            "appkey" : AppKey,
            'categoryId' : '',
            'skillOrigin' : '',
        },
        "output" : "name=音乐点播（酷我",
    },

    # 4. 技能详情
    "rest/v1/skill/detail" : {
        "input" : {
            "skillId" : SkillId,
            'skillVersion' : SkillVersion,
        },
        "output" : "skillId=%s;name=自动化" % SkillId,
    },

    # 5. 技能关联App数量
    "rest/v1/skillApp/count" : {
        "input" : {
            "skillId" : SkillId,
            'skillVersion' : SkillVersion,
        },
        "output" : "errorMsg=请求成功",
    },

    # 6. 技能关联App信息
    "rest/v1/skillApp/skill_app_info" : {
        "input" : {
            "skillId" : SkillId,
            'skillVersion' : SkillVersion,
        },
        "output" : "name=自动化测试",
    },
}
#############
def run_case(api, url, params, output):
    global Result_Map
    ret = send_get(url, params)
    unmatchs = get_unmatch_list(output, ret)
    result = "Pass"
    if unmatchs:
        result = 'Fail'
        print "Fail %s" % api
        print unmatchs
        print '-' * 40       
    # else:
    #     print "Pass %s" % api
    Result_Map[result].append(api)

def check_api(api=None):
    USK_API_DATA = init_test_data()
    if api is not None:
        url = "http://%s/%s/%s" % (Server, Project, api)
        params = USK_API_DATA[api]['input']
        output = USK_API_DATA[api]['output']
        run_case(api, url, params, output)
        return 
    for api, case in USK_API_DATA.items():
        url = "http://%s/%s/%s" % (Server, Project, api)
        params = USK_API_DATA[api]['input']
        output = USK_API_DATA[api]['output']
        run_case(api, url, params, output)

def output():
    pass_api = Result_Map["Pass"]
    fail_api = Result_Map["Fail"]
    total = len(pass_api) + len(fail_api)
    print '----------------------------------------------'
    for api in pass_api:
        print 'Pass >> %s' % api
    print '----------------------'
    for api in fail_api:
        print 'Fail >> %s' % api
    print '----------------------------------------------'
    print "Total: %d, Pass: %d Fail: %d" % (total, len(pass_api), len(fail_api))





if __name__ == '__main__':
    
    Project = "unios-usk-api"
    Result_Map = {"Pass" : [], "Fail" : []}
    
    # env = 'onl'
    env = 'uat'

    Server = "172.18.10.235:8080" # UAT
    if env != 'nat':
        Server = "172.18.10.235:8080" # 生产环境

    env_vars = get_env_vars(env)
    AppKey = env_vars['appkey']
    SkillId = env_vars['skill_id']
    SkillVersion = '1.0.19'

    UDID = 'dakang-test-udid'
    # SkillId = '84'
    # skillId = 'cn.yunzhisheng.chat'
    # skillId = 'c3494ff147224ad686e0c7880ac7dc3b' # 成语接龙
    # skillId = '03d5254078ef4e2cb5956bd9e593e0c2' # 游戏 猜拳
    # SkillVersion = "1.0.8"

    api = 'rest/v1/skill/hot_list'
    api = 'rest/v1/skill/guess_list'
    api = 'rest/v1/skill/query'
    api = 'rest/v1/skill/detail'
    api = 'rest/v1/skillApp/count'
    api = 'rest/v1/skillApp/skill_app_info'
    api = None
    check_api(api)
    output()



    #################################
    # hot_skill()
    # guess_skill()

    # query_skill()
    # detail_skill()
    # skill_app_count()
    # skill_app_info()